package com.zking.controller.jch;

import com.zking.entity.Brand;
import com.zking.entity.Specification;
import com.zking.service.jch.SpecificationService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.servlet.http.HttpServletRequest;
import java.util.List;

@Controller
@RequestMapping("/Specification")
public class SpecificationController {
    @Autowired
    private SpecificationService specificationService;

    @RequestMapping(value = "/selectSpecificationAll",method = RequestMethod.GET,name = "规格查询")
    public String SelectAllSpecification(HttpServletRequest request){
        List<Specification> specifications = specificationService.queryAllSpecifications();
        request.setAttribute("specifications",specifications);
        return "jch/specification";
    }

    @RequestMapping(value = "/addSpecification",method = RequestMethod.GET,name = "添加规格")
    public String AddSpecification(HttpServletRequest request){
        String specName = request.getParameter("specName");
        boolean b = specificationService.addSpecification(specName);
        return "redirect:selectSpecificationAll";

    }

    @RequestMapping(value = "delSpecification",method = RequestMethod.GET, name = "删除规格")
    public String delSpecification(HttpServletRequest request){
        String ids = request.getParameter("ids");
        if (ids == null || ids.isEmpty()){
            return "redirect:selectSpecificationAll";
        }

        String[] split = ids.split(",");
        int length = split.length;
        int[] id = new int[length];
        System.out.println(split);
        for (int i = 0; i < length ; i++) {
            id[i] = Integer.parseInt(split[i]);
            boolean b = specificationService.delSpecification(id[i]);

            System.out.println(b);
        }

        return "redirect:selectSpecificationAll";
    }

    @RequestMapping("/data")
    @ResponseBody
    public Specification getData(int id) {
        Specification specification = specificationService.querySpecification(id);
        return specification;
    }

    @RequestMapping(value = "/updateSpecification", method = RequestMethod.GET,name = "更新规格" )
    public String updateBrand(HttpServletRequest request) {
        String specName2 = request.getParameter("specName2");

        int upid = Integer.parseInt(request.getParameter("upid"));
        boolean b = specificationService.updateSpecification(upid, specName2);
        System.out.println(b);
        return "redirect:selectSpecificationAll";

    }
}
